//
//  YeeShopDetailVC.m
//  YeeShopProject
//
//  Created by CoderYee on 2017/3/3.
//  Copyright © 2017年 CoderYee. All rights reserved.
//

#import "YeeShopDetailVC.h"
#import "YeeShopHeaderView.h"//头部轮播图
#import "YeeShopToolBottomView.h"//底部工具栏
#import "YeeShopDetailSubsVC.h"
#import "YeeCarSelectedSuitVC.h" //套装id
@interface YeeShopDetailVC ()
{
    UIView               *_headerView;
    YeeShopDetailSubsVC  *_ShopDetailSubsVC;
    YeeShopToolBottomView *_toolView;
}
@end

@implementation YeeShopDetailVC

- (void)viewDidLoad
{
    [super viewDidLoad];
}
-(void)addOwnViews
{
    [super addOwnViews];
    [self addheaderView];
    [self addrefreshFootView];
    [self addBottomToolView];
}
-(void)addNavbarView
{
    [super addNavbarView];
    self.title=@"商品详情";
    self.navigationItem.rightBarButtonItem=[UIBarButtonItem itemWithBtnimage:@"icon_gouwuche_weidianji" target:self action:@selector(clickrightButton)];
}
-(void)configOwnViews
{
    [super configOwnViews];
    self.tableView.frame=CGRectMake(0, 0, kMainScreenWidth, kcontentViewHeight-40);
    [self.tableView setBackgroundColor:xBackgroundColor];
    //这里需要添加刷新
}
-(void)addheaderView
{
    YeeShopHeaderView *headerView=[[YeeShopHeaderView alloc] initWithFrame:CGRectMake(0, 0, kMainScreenWidth, kMainBannerHight+100)];
    [headerView setBackgroundColor:kWhiteColor];
    //需要计算高度
    self.tableView.tableHeaderView=headerView;
}

-(void)addrefreshFootView
{
    //这里需要动态更改view的位置
    UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, self.tableView.frame.size.height, kMainScreenWidth, 40)];
    [view setBackgroundColor:xBackgroundColor];
    UILabel *lable=[[UILabel alloc] initWithFrame:CGRectMake(0, 10, kMainScreenWidth, 21)];
    lable.textAlignment=NSTextAlignmentCenter;
    lable.text=@"上拉刷新获取更多精彩";
    lable.font=[UIFont systemFontOfSize:14];
    _headerView=view;
    [view addSubview:lable];
    [self.tableView addSubview:view];
}
-(void)addBottomToolView
{
    YeeShopToolBottomView *toolView=[[YeeShopToolBottomView alloc] initWithFrame:CGRectMake(0,kcontentViewHeight-40 , kMainScreenWidth, 40)];
    @weakify(self);
    [toolView .clickSubject subscribeNext:^(NSString  *string)
    {
      @strongify(self);
       YeeCarSelectedSuitVC *suitVc=[YeeCarSelectedSuitVC new];
       [self.view.window addSubview:suitVc.view];
       [self addChildViewController:suitVc];
    }];
    _toolView=toolView;
    [self.view addSubview:toolView];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class])];
    if (cell==nil)
    {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([ UITableViewCell class])];
    }
    cell.textLabel.text=@"CoderYee";
    return cell;
}
-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view=[[UIView alloc] init];
    return view;
}
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, kMainScreenWidth, 0.01)];
    return view;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}
#pragma mark 重写调整frame
- (void)adjustFrame
{
    // 内容的高度
    CGFloat contentHeight = self.tableView.contentSize.height;
    // 表格的高度
    CGFloat scrollHeight = self.tableView.frame.size.height;
    CGFloat y = MAX(contentHeight, scrollHeight);
    // 设置边框
    _headerView.frame= CGRectMake(0, y, kMainScreenWidth, 40);
    
    //这里需要pop一个动画
    if (self.tableView.contentOffset.y+self.tableView.frame.size.height>y+40)
    {
        
            
            if (_ShopDetailSubsVC==nil)
            {
                _ShopDetailSubsVC= [[YeeShopDetailSubsVC alloc ] init];
                [_ShopDetailSubsVC .view setFrame:CGRectMake(0, kMainScreenHeight, kMainScreenWidth, kcontentViewHeight-20)];
                [self.view addSubview:_ShopDetailSubsVC.view];
//                [_ShopDetailSubsVC.view setBackgroundColor:[UIColor greenColor]];
                [self addChildViewController:_ShopDetailSubsVC];
                [self.view bringSubviewToFront:_toolView];
            }
            //self.tableView.contentOffset = CGPointMake(0, 0);
        [UIView animateWithDuration:0.8 animations:^{
            [_ShopDetailSubsVC.view setFrame:CGRectMake(0, 0, kMainScreenWidth, kcontentViewHeight-20)];
        }];
        
    }

}

#pragma -- <UIScrollViewDelegate>
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //CGFloat h = self.tableView.frame.size.height;
    CGFloat f= self.tableView.contentOffset.y ;

    CGFloat mar= self.tableView.contentSize.height ;
    //CGFloat f= self.tableView.contentSize.height ;

    NSLog(@" --== %f",f);
    NSLog(@"12343 --== %f",mar);

    [self adjustFrame];
    //需要根据44判断
    //获取内容超过
}
-(void)clickrightButton
{
    
    
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
